<?php
/**
 * Created by PhpStorm.
 * User: 姚荣
 * Date: 2017/5/27
 * Time: 17:57
 */
namespace app\common\controller;

use think\Lang;
use think\Controller;
use app\common\model\Config;
use think\Request;

class homebase extends Controller
{
    protected $language;
    protected $langstr;
    public function __construct(Request $request)
    {
        parent::__construct();
        if (empty(cookie('think_var'))){
            Lang::detect();
        }
        if (!is_mobile()) {
            $this->view->config('view_path', '../app/home/view/web/');
        } else {
            $this->view->config('view_path', '../app/home/view/mobile/');
        }
        $this->cate    = model('Cat');
        $this->pcate    = model('Pcate');
        $this->language=cookie('think_var');
        $this->langstr=$this->get_lang();
        $this->assign('langue',$this->langstr);
        $this->get_config();
        $this->assign('action', strtolower($request->action()));
        $this->assign('controller', strtolower($request->controller()));
        $procate=$this->pcate ->where(['pid'=>0])->order('sort desc')->select();
        $pcate_ret=[];
        foreach ($procate as $vo){
            $cat['top']=$vo;
            $cat['list']=$this->pcate ->where(['pid'=>$vo['id']])->order('sort desc')->select();
            array_push($pcate_ret,$cat);
        }
        $this->assign('procate',$pcate_ret);
    }

    public function lang() {
        $this->language=input('lang');

        switch ($this->language) {	
            case 'cn':
                cookie('think_var', 'zh-CN');
                break;
            case 'en':
                cookie('think_var', 'en-US');
                break;
        }
    }

    protected function get_lang(){
        if (is_string($this->language)){
            $langstr=substr($this->language , 0 , 2);
            return $langstr;
        }else{
            $this->language=settype($this->language,"string");
            $langstr=substr($this->language , 0 , 2);
            return $langstr;
        }
    }

    protected function get_config()
    {
        $config=new config();
        //根据语种变化设置不同语种地址
        $address=$config->where('config','address_'.$this->langstr)->find()['val'];
        $this->assign('config_address',$address);

        $config_list=$config->where('config','notlike','%zh')->where('config','notlike','%en')->select();
        foreach ($config_list as $value){
            $this->assign('config_'.$value['config'],$value['val']);
        }
    }


}
